﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic;

namespace CygwinPackageManager.Common
{
	public static class Extensions
	{
		public static string ToSingleLine(this string s)
		{
			return (string.IsNullOrEmpty(s))
				? s
				: s.Replace(ControlChars.CrLf, " ")
					.Replace(ControlChars.Cr, ' ')
					.Replace(ControlChars.Lf, ' ');
		}

		/// <summary>
		/// Removes all leading and trailing white-space, comma and control characters 
		/// from the current System.String object.
		/// </summary>
		/// <param name="s"></param>
		/// <returns>
		/// The string that remains after all white-space, comma and control characters 
		/// are removed from the start and end of the current System.String object.
		/// </returns>
		public static string ExtendedTrim(this string s)
		{
			if (string.IsNullOrEmpty(s))
				return s;

			char[] controlChars = new char[] 
			{ 
				ControlChars.Back, 
				ControlChars.Cr, 
				ControlChars.FormFeed, 
				ControlChars.Lf, 
				ControlChars.NullChar,
				ControlChars.Quote,
				ControlChars.Tab,
				ControlChars.VerticalTab,
			};

			return s.Trim(new char[] { ' ', ',' }).Trim(controlChars);
		}
	}
}
